/**
 * Copyright (C) 2013 Oleg Anastassov <oleg.anastassov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.loc.desktop.settings;

import kz.kstu.loc.desktop.AppConfig;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Date: 25.01.2013
 * Time: 16:53
 *
 * @author Oleg Anastassov
 */
public class SettingsManager {

    private static final String SETTINGS_PATH = "settings.properties";

    public static final String PREFIX = "loc.tool.process.prefix";

    private final Map<String, String> DEFAULT_VALUES = new HashMap<String, String>() {
        {
            put(PREFIX, "^");
        }
    };

    private static SettingsManager instance;

    private Properties settings;

    public SettingsManager() {
        settings = loadSettings();
    }

    public static SettingsManager getInstance() {
        if (instance == null) {
            instance = new SettingsManager();
        }
        return instance;
    }

    public String getProperty(String key) {
        return settings.getProperty(key);
    }

    public void setProperty(String key, String value) {
        settings.setProperty(key, value);
    }

    public boolean saveToDisk() {
        File f = new File(SETTINGS_PATH);
        try {
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8"));
            if (!f.exists()) {
                f.createNewFile();
            }
            settings.store(out, AppConfig.getInstance().getAppName());
            return true;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    private Properties loadSettings() {
        Properties p = new Properties();
        try {
            InputStream is = new FileInputStream(SETTINGS_PATH);
            p.load(is);
        } catch (IOException e) {
            p = defaults();
        }
        return p;
    }

    private Properties defaults() {
        Properties p = new Properties();
        p.putAll(DEFAULT_VALUES);
        return p;
    }

}
